#coding:utf-8
__author__ = 'Administrator'
import urllib,urllib2,cookielib

#通过网页登录163邮箱
class User163:
    #伪装浏览器
    header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    username=''
    password=''
    cookie=None
    user=''

    def __init__(self,name,pwd):
        self.username=name
        self.password=pwd
        self.cookie = cookielib.LWPCookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
        urllib2.install_opener(opener)

    def login(self):
        #请求参数
        post_data = {'username':self.username,
                     'password':self.password,
                     'type':1}
        post_data = urllib.urlencode(post_data)

        #发起请求
        request = urllib2.Request(url='http://reg.163.com/logins.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D1',
                                  data=post_data,
                                  headers=self.header)

        response = urllib2.urlopen(request).read()
        result = str(response)
        if '登录成功，正在跳转' in result:
            print(self.username+'成功登录')
        else:
            print('登录失败')


#使用测试
if __name__ == '__main__':
  user = User163('13526519085@163.com','8382117')
  user.login()

  user = User163('13526519084@163.com','8382117')
  user.login()